package datastructure.ch03_linked.ch12_double_circle;

import datastructure.ch03_linked.ch07_nodes.DoubleNode;
import datastructure.ch03_linked.ch08_interface_abstract.AbstractLinkedList;

/**
 * 双向循环列表
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
public class DoubleCircleLinkedList<E> extends AbstractLinkedList<E> {
    public static void main(String[] args) {
        DoubleCircleLinkedList<Integer> data = new DoubleCircleLinkedList<>();
        data.add(0, 1);
        data.add(1, 2);
        data.add(2, 3);
        data.add(3, 4);
        data.remove(0);
        System.out.println(data);
    }

    DoubleNode<E> first;
    DoubleNode<E> last;

    @Override
    public int indexOf(E element) {
        DoubleNode<E> node = first;
        if (element == null) {
            for (int i = 0; i < size; i++) {
                if (node.element == null) {
                    return i;
                }
            }
        } else {
            for (int i = 0; i < size; i++) {
                if (element.equals(node.element)) {
                    return i;
                }
            }
        }
        return ELEMENT_NOT_FOUND;
    }

    @Override
    public void clear() {
        size = 0;
        first = null;
        last = null;
    }

    @Override
    public void add(int index, E element) {
        rangeCheckForAdd(index);
        if (index == size) {
            DoubleNode<E> oldLast = last;
            last = new DoubleNode<>(oldLast, element, first);
            if (oldLast == null) {
                first = last;
                first.next = first;
                first.prev = first;
            } else {
                oldLast.next = last;
                // last.next = first;
                first.prev = last;
            }
        } else {
            DoubleNode<E> next = getNode(index);
            DoubleNode<E> prev = next.prev;
            DoubleNode<E> current = new DoubleNode<>(prev, element, next);

            next.prev = current;
            prev.next = current;
            if (next == first) { // index == 0
                first = current;
            }
        }
        size++;
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);
        DoubleNode<E> current = first;
        if (size == 1) {
            first = null;
            last = null;
        } else {
            current = getNode(index);
            DoubleNode<E> prev = current.prev;
            DoubleNode<E> next = current.next;
            prev.next = next;
            next.prev = prev;
            // 删除头节点
            if (current == first) { // index == 0
                first = next;
            }

            // 删除尾节点
            if (current == last) { // index = last-1
                last = prev;
            }
        }
        size--;
        return current.element;
    }

    @Override
    public E set(int index, E element) {
        return null;
    }

    @Override
    public E get(int index) {
        return null;
    }

    private DoubleNode<E> getNode(int index) {
        DoubleNode<E> node;
        if (index < (size >> 1)) {
            node = first;
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
        } else {
            node = last;
            // 思考？i为啥不能=index
            for (int i = size - 1; i > index; i--) {
                node = node.prev;
            }
        }
        return node;
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        DoubleNode<E> node = first;
        for (int i = 0; i < size; i++) {
            result.append(node);
            if (i < size - 1) result.append("->");
            node = node.next;
        }
        return result.toString();
    }
}
